//
//  main.m
//  Demo0025
//
//  Created by zTercel on 13-3-7.
//  Copyright (c) 2013年 zTercel. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Book : NSObject<NSCopying>
{
    NSMutableString*   _name;
    NSString*   _isbn;
    NSUInteger  _price;
}
@property(nonatomic, copy) NSString*    _name;
@property(nonatomic, copy) NSString*    _isbn;
@property(nonatomic, assign) NSUInteger _price;

-(id) init;
-(void) print:(NSString*)prompt;
-(id) copyWithZone:(NSZone*)zone;
@end


@implementation Book

@synthesize _name;
@synthesize _isbn;
@synthesize _price;

-(id) init
{
    if (self = [super init])
    {
        _name   = @"";
        _isbn   = @"";
        _price  = 0;
    }
    return self;
}

-(void) print:(NSString*)prompt
{
    NSLog(@"\n%@\nname: %@\nisbn: %@\nprice: %lu", prompt, _name, _isbn, _price);
}

-(id) copyWithZone:(NSZone*)zone
{
    Book* book = [[Book allocWithZone:zone]init];
    
    [book set_name:_name];
    [book set_isbn:_isbn];
    [book set_price:_price];
    
    return book;
}

@end


int main(int argc, const char * argv[])
{
    @autoreleasepool {
        Book* book = [[Book alloc]init];
        NSMutableString* name = [NSMutableString stringWithString:@"ztercel objective-c"];
        [book set_name:name];
        [book set_isbn:@"978-7-126-14974-10"];
        [book set_price:89.0];
        [book print:@"create book:"];
        
        [name appendFormat:@"i do"];  
        [book print:@"after modify name source data, book data:"];

        Book* book2 = [book copy]; // book;
        [book2 set_name:@"objective-c programming"];
        [book2 set_price:100];
        [book2 print:@"book2 value: "];
        [book print:@"book value: "];
        
        
        // 浅复制
        {
            NSLog(@"\n========assign=============");
            NSMutableArray* array1 = [NSMutableArray arrayWithObjects:@"string1",@"string2", @"string3", @"string4", nil];
            NSMutableArray* array2 = array1;   
            NSLog(@"array1 = %@\narray2 = %@", array1, array2);
            
            [array2 removeObjectAtIndex:0];
            NSLog(@"array1 = %@\narray2 = %@", array1, array2);
        }
         
        // 深复制
        {
            NSLog(@"\n========copy=============");
            NSMutableArray* array1 = [NSMutableArray arrayWithObjects:@"string1",@"string2", @"string3", @"string4", nil];
            NSMutableArray* array2 = [array1 mutableCopy];
            NSLog(@"array1 = %@\narray2 = %@", array1, array2);
            
            [array2 removeObjectAtIndex:0];
            NSLog(@"array1 = %@\narray2 = %@", array1, array2);
        }
       
    }
    return 0;
}

